/**
 * 持卡人
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="margin-t10">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>持卡人列表</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="boxBlock">
      <div class="serachTop">  
        <!-- 搜索筛选 -->
        <el-form :inline="true" :model="formInline" class="user-search">
          <el-form-item>
            <el-input
              size="small"
              v-model="formInline.lastName"
              placeholder="输入持卡人姓"
              clearable
            ></el-input>
          </el-form-item>
       
          <el-form-item >
              <el-input
                size="small"
                v-model="formInline.userId"
                placeholder="请输入使用者ID"
                v-if='hasPermission'
                type="number"
                clearable
              ></el-input>
            </el-form-item> 
          
          <el-form-item label="">
            <el-select
              size="small"
              v-model="formInline.status"
              placeholder="请选择持卡人状态"
              clearable
            >
              <el-option label="请选择持卡人状态" value=""></el-option>
              <el-option
                v-for="type in statusHolder"
                :label="type.key"
                :value="type.value"
                :key="type.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="">
            <el-select
              size="small"
              v-model="cardType" 
            > 
              <el-option 
                label="常规卡持卡人"
                :value="1" 
              ></el-option>
              <el-option 
                label="共享卡卡持卡人" 
                :value="2" 
              ></el-option>
            </el-select>
          </el-form-item>


          <el-form-item>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="search"
            >
              搜索
            </el-button>
            <!-- <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              @click="handleEdit()"
              >添加持卡人</el-button
            > -->
          </el-form-item>
        </el-form>
      </div>
      
      <!--列表-->
    <div class="ra_box">
      <div class="margin-r20 margin-b10">
        <el-button
          size="small"
          type="danger"
          @click="stopHolder('停用', 'disabled')"
          :disabled="changeIDList.length == 0"
          v-if="changeIDList.length>0"
          class="deepC"
        >
          批量停用
        </el-button>
      </div>
      <el-table
        size="small"
        :data="listData"
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        style="width: 100%"
        v-if="listData"
        @selection-change="toggleSelection"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
             prop="parentUserId"
            label="所有者ID"
            show-overflow-tooltip
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="userId"
            label="使用者ID"
            show-overflow-tooltip
            width="120"
          >
         </el-table-column>
        <el-table-column prop="lastName" label="持卡人姓" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="firstName"
          label="持卡人名字"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="nationalityCountryCode"
          label="国籍"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ n(scope.row.nationalityCountryCode) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="账单国家" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ n(scope.row.residentialCountryCode) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ jsonHolder[scope.row.status] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="创建时间"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ timestampToTime(scope.row.createdAt, 8 * 60 * 60) }}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
               size="mini"   type="success" plain
              @click="handleEdit(scope.$index, scope.row)" :key="scope.$index"
              v-if="hasPermission6"
            >
              修改
            </el-button>
            <el-button
               size="mini"   type="danger" plain
              :disabled="scope.row.status == 'disabled'"
              @click="stopHolder('停用', 'disabled', scope.row)"
              :key="scope.$index"
              v-if="hasPermission6"
            >
              停用
            </el-button>
 
            <el-button
              size="mini"
              type="success" 
              @click="handleStart('启用', 'normal', scope.row)"
            >
              启用
            </el-button> 
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>


    </div>
    

    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="45%"
      @close="closeDialog"
      class="editBox"
      :close-on-click-modal="false"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm" label-suffix=""
        
      >
        <el-form-item
          label="国籍"
          prop="nationalityCountryCode" 
        >
          <el-select
            size="small"
            v-model="editForm.nationalityCountryCode"
            placeholder="请选择国籍"
            filterable
          >
            <el-option
              v-for="(type, index) in nation"
              :label="type.label"
              :value="type.value"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="持卡人名字" prop="firstName" >
          <el-input
            size="small"
            v-model="editForm.firstName"
            auto-complete="off"
            placeholder="请输入持卡人名字"
          ></el-input>
        </el-form-item>
        <el-form-item label="持卡人姓" prop="lastName">
          <el-input
            size="small"
            v-model="editForm.lastName"
            auto-complete="off"
            placeholder="请输入持卡人姓"
          ></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="dateOfBirth" >
          <el-date-picker
            size="small"
            v-model="editForm.dateOfBirth"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" >
          <el-input
            size="small"
            v-model="editForm.email"
            auto-complete="off"
            placeholder="请输入持卡人邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            placeholder="请输入手机号"
            v-model="editForm.mobile"
            class="input-with-select"
          >
            <el-select
              v-model="editForm.mobilePrefix"
              slot="prepend"
              placeholder="请选择手机号前缀"
              class="mobilePrefix"
              style="width: 140px;"
              filterable
            >
              <el-option
                v-for="(type, index) in phoneInfo"
                :label="type.label"
                :value="type.value"
                :key="index"
              ></el-option>
            </el-select>
          </el-input>
          <el-tag type="danger">
            <i class="el-icon-warning-outline font12">
              请如实填写正确的手机号和邮箱，特殊业务将会通过该字段发送验证码
            </i>
          </el-tag>
         
        </el-form-item>
        <el-form-item
          label="账单地国家"
          prop="residentialCountryCode"
          class=" select10"
        >
          <el-select
            size="small"
            v-model="editForm.residentialCountryCode"
            placeholder="请选择国家"
            filterable
          >
            <el-option
              v-for="type in nation"
              :label="type.label"
              :value="type.value"
              :key="type.value"
              @click.native="getChild(type)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="账单地州省" prop="residentialState">
          <el-select
            size="small"
            v-model="editForm.residentialState"
            placeholder="请选择账单地州省"
            filterable
            v-if="dentialState"
          >
            <el-option
              v-for="(type, index) in dentialState"
              :label="type.value"
              :value="type.value"
              :key="index"
            ></el-option>
          </el-select>
          <el-input
            size="small"
            v-model="editForm.residentialState"
            auto-complete="off"
            placeholder="请输入账单地州省"
            v-else
          ></el-input>
        </el-form-item>
        <el-form-item label="账单城市" prop="residentialCity" >
          <el-input
            size="small"
            v-model="editForm.residentialCity"
            auto-complete="off"
            placeholder="请输入账单城市"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="账单详细地址"
          prop="residentialAddress"
         
        >
          <el-input
            size="small"
            v-model="editForm.residentialAddress"
            auto-complete="off"
            placeholder="请输入账单详细地址"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="账单地邮编"
          prop="residentialPostalCode"
           
        >
          <el-input
            size="small"
            v-model="editForm.residentialPostalCode"
            auto-complete="off"
            placeholder="请输入账单地邮编"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" >
          <el-input
            type="textarea"
            size="small"
            v-model.trim="editForm.remark"
            :rows="5"
            auto-complete="off"
            placeholder="备注"
          ></el-input>

          <i class="el-icon-warning-outline font12">
            修改持卡人信息后会影响过往持卡人所有开通过的卡片信息
          </i>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import {
  setStoreS,
  getStoreS,
  removeStoreS,
  insertCharacter,
  timestampToTime,
  timestampToData,
  phoneReg,
  emailReg,
  spStr,
} from "../../utils/util";
import {
  phoneInfo,
  nation,
  getPhone,
  getNation,
  USzhou,
} from "../../utils/cardData";
import { baseURL, service, format, getUrl } from "../../api/axiosFun";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false,
      addFormVisible: false,
      editFormVisible: false,
      b: format,
      timestampToTime: timestampToTime,
      p: getPhone,
      n: getNation,
      dentialState: null,
      phoneInfo: phoneInfo,
      nation: nation,
      title: "添加",
      editForm: {
        dateOfBirth: "",
        email: "",
        firstName: "",
        lastName: "",
        mobile: "",
        mobilePrefix: "86",
        nationalityCountryCode: "",
        remark: "",
        residentialAddress: "",
        residentialCity: "",
        residentialCountryCode: "",
        residentialPostalCode: "",
        residentialState: "",
      },
      // rules表单验证
      rules: {
        dateOfBirth: [
          { required: true, message: "请选择出生日期", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入持卡人邮箱", trigger: "blur" },
          {
            pattern: emailReg,
            required: true,
            message: "请输入正确的邮箱",
            trigger: "blur",
          },
        ],
        firstName: [
          { required: true, message: "请输入持卡人名字", trigger: "blur" },
        ],
        lastName: [
          { required: true, message: "请输入持卡人姓", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: phoneReg,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        nationalityCountryCode: [
          { required: true, message: "请选择国籍", trigger: "change" },
        ],
        residentialAddress: [
          { required: true, message: "请输入详细的账单地址", trigger: "blur" },
        ],
        residentialCity: [
          { required: true, message: "请输入账单城市", trigger: "blur" },
        ],
        residentialCountryCode: [
          { required: true, message: "请选择账单地国家", trigger: "change" },
        ],
        residentialPostalCode: [
          { required: true, message: "请输入账单地邮编", trigger: "blur" },
        ],
        residentialState: [
          { required: true, message: "请输入账单地州省", trigger: "blur" },
        ],
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        lastName: "",
        status: "",
      },
      // 删除
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      changeIDList: [],
      statusHolder: [
        { key: "正常", value: "normal" },
        { key: "停用", value: "disabled" },
      ],
      jsonHolder: {
        normal: "正常",
        disabled: "停用",
      },
      hasPermission6:false,
      hasPermission:false,
      cardType:1

    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  computed: {
    ...mapGetters(["getUser"]), 
  },
  watch: {
    getUser(newVal) {
      this.userInfo = JSON.parse(newVal);
      this.hasPermission6 = this.$hasPermission(this.userInfo,['common','user6']) 
      this.hasPermission = this.$hasPermission(this.userInfo,['common'])  
    },
  }, 
  created() { 
    if(this.$store.state.user){
      this.userInfo = JSON.parse(this.$store.state.user);
      this.hasPermission6 = this.$hasPermission(this.userInfo,['common','user6']) 
      this.hasPermission = this.$hasPermission(this.userInfo,['common'])  
    }
    this.getdata(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    hasPermissionMethod(data) { 
      this.userInfo = JSON.parse(this.$store.state.user);
      return this.$hasPermission(this.userInfo,data) 
    },
    getdata(parameter) {
      let url = getUrl(parameter);
      service
        .get("/api/photon/cardHolderList" + url, {})
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.listData = res.rows;
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.total;
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    getChild(e) {
      this.dentialState = e.child;
      this.editForm.residentialState = "";
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {  
      if(this.userInfo){
        this.formInline.matrixAccount=this.cardType==1?'':this.userInfo.matrixAccount; 
      }
      console.log(this.formInline,'this.formInline');
      this.getdata(this.formInline);
    },
    toggleSelection(rows) {
      this.changeIDList = [];
      rows.forEach((i) => {
        this.changeIDList.push(i.id);
      });
    },
    stopHolder(name, status, row) {
      this.$confirm("请问您确定要" + name + "该持卡人吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let parms = {
              ids: this.changeIDList.toString(), 
              status: status, 
            },
            url;

          if (row) {
            parms.ids = row.id;
          }
          url = getUrl(parms);
          service
            .post("/api/photon/disableHolder" + url)
            .then((res) => {
              if (res.code == 0) {
                this.$message.success("操作成功，数据处理中...");
                this.getdata(this.formInline);
              } else {
                this.$message.error(spStr(res.msg, "PhotonPay", ""));
                return;
              }
            })
            .catch((err) => {
              this.$message.error(spStr(err, "PhotonPay", ""));
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消" + name + "卡",
          });
        });
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.editForm = row;
        if (row.residentialCountryCode == "US") {
          this.dentialState = USzhou;
        }
      } else {
        this.title = "添加持卡人";
        this.editForm = {
          dateOfBirth: "",
          email: "",
          firstName: "",
          lastName: "",
          mobile: "",
          mobilePrefix: "86",
          nationalityCountryCode: "",
          remark: "",
          residentialAddress: "",
          residentialCity: "",
          residentialCountryCode: "",
          residentialPostalCode: "",
          residentialState: "",
        };
      }
    },
    checkUser(index, row) {},
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          this.editForm.dateOfBirth = timestampToData(
            this.editForm.dateOfBirth
          );
          service
            .post("/api/photon/cardHolderEdit", this.editForm)
            .then((res) => {
              this.loading = false;
              if (res.code == 0) {
                this.getdata(this.formInline);
                this.closeDialog();
              } else {
                this.$message({
                  type: "info",
                  message: spStr(res.msg, "PhotonPay", ""),
                });
              }
            })
            .catch((err) => {
              this.$message.error(spStr(err, "PhotonPay", ""));
            });
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
      this.$refs.editForm.resetFields();
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.editBox .el-form-item {
  /* display: inline-block; */
}
.editBox .select10 .el-select,
.editBox .el-date-editor {
  width: 100%;
}
.editBox .mobilePrefix {
  width: 110px;
}
.userRole {
  width: 100%;
}
</style>

 
 